<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="质检编号" prop="checkCode">
        <el-input
          v-model="queryParams.checkCode"
          placeholder="请输入质检编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品批次" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品批次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="checkId" />
      <el-table-column label="质检编号" align="center" prop="checkCode" />
      <el-table-column label="检测项型" align="center" prop="testItemType" />
      <el-table-column label="产品批次" align="center" prop="materialCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="检测日期" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" align="center" prop="result">
        <template v-slot="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
          >开始运输</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质检对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批次编号" prop="materialLotId">
          <el-select v-model="form.materialLotId" placeholder="批次编号">
            <el-option
              v-for="item in materialLots"
              :key="item.materialLotId"
              :label="item.materialCode"
              :value="item.materialLotId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="检测结果" prop="delFlag">
          <el-select v-model="form.result" placeholder="请输入检测结果">
            <el-option
              v-for="item in dict.type.sys_result"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="pickerValue"
            :picker-options="pickerOptions"
            type="datetimerange"
            range-separator="至"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 结束质检对话框 -->

    <!-- 开始运输通知司机 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisibles" style="width: 1000px;margin-left: 400px">
      <el-form :model="checkForm" >
        <el-row>
          <el-col :span="12">
              <el-form-item label="请选择司机" v-if="driverName===undefined">
                <el-button
                  v-model="toDriveId"
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="openAddDrive(toDriveId)"
                >请选择司机</el-button>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请选择司机"  v-if="driverName!==undefined">
              <el-tag  v-model="driverName">{{driverName}}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="请选择仓库">
          <el-select
            filterable
            popper-class="t_pagination_select"
            v-model="checkForm.warehouseId"
            :style="{width: width||'100%'}"
            v-bind="checkForm.warehouseId"
            v-on="$listeners"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.warehouseId"
              :label="item.warehouseName"
              :value="item.warehouseId"
            ></el-option>
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams1.pageNum"
              :limit.sync="queryParams1.pageSize"
              @pagination="getWarehouse"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="运输区间" prop="beginTime">
          <el-date-picker
            v-model="timeValue1"
            :picker-options="pickerOptions1"
            type="datetimerange"
            range-separator="至"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取 消</el-button>
        <el-button type="primary" @click="submitYunShu">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 结束运输通知司机 -->


    <!--开始司机对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisibleDrive">
      <ToDrive :drive="drive" @close="closeDrive"></ToDrive>
    </el-dialog>
    <!--结束司机对话框-->

  </div>
</template>

<script>
import { listCheck, getCheck, delCheck,addCheck, updateCheck,addYunJiance } from "@/api/quality/Incoming";
import {getMaterialLotList} from "@/api/product/products";
import {getDriverList, getWarehouseList2} from "@/api/crop/products";
import ToDrive from "@/views/quality/Incoming/ToDrive";
import cropIng from "@/store/modules/cropIng";
import user from "@/store/modules/user";

export default {
  components:{ToDrive},
  name: "index",
  dicts: ['sys_result'],
  data() {
    return {
      width:undefined,
      childSelectedValue:undefined,
      // 遮罩层
      toDriveId:'ToDrive',
      loading: true,
      driverName: undefined,
      dialogFormVisibles: false,
      dialogFormVisibleDrive: false,
      // 选中数组
      ids: [],
      drive: [],
      driverList:[],
      warehouseList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检表格数据
      checkList: [],
      materialLots: [],
      checkForm: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkCode: null,
        testItemType: null,
        productCode: null,
        productName: null,
        warehouseName: null,
        userId:undefined,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      //检测时间
      timeValue: [],
      timeValue1: [],
      pickerValue: [],
      pickerOptions:{
        disabledDate(time){
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      pickerOptions1:{
        disabledDate(time){
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeDrive(){
      console.log(cropIng.state.driverId,"成羽欣美女")
      console.log(cropIng.state.driverName,"成羽欣美女")
      this.driverName=cropIng.state.driverName;
      this.dialogFormVisibleDrive=false;
    },
    openAddDrive(){
      this.dialogFormVisibleDrive=true;
    },
    /** 查询批次号列表 */
    selectMaterialLotList(){
      getMaterialLotList().then(response => {
        this.materialLots = response.data;
      });
    },
    /** 查询质检列表 */
    getList() {
      this.loading = true;
        this.queryParams.zjId=user.state.id;
        listCheck(this.queryParams).then(response => {
          this.checkList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form={};
      this.pickerValue=[];
      this.selectMaterialLotList();
      this.title = "添加质检";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form={};
      this.pickerValue=[];
      if(row.beginTime!=undefined || row.endTime!=undefined){
        this.pickerValue.push(row.beginTime);
        this.pickerValue.push(row.endTime);
      }
      this.selectMaterialLotList();
      const checkId = row.checkId || this.ids
      getCheck(checkId).then(response => {
        this.title = "修改质检";
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            this.form.beginTime=this.pickerValue[0];
            this.form.endTime=this.pickerValue[1];
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.beginTime=this.pickerValue[0];
            this.form.endTime=this.pickerValue[1];
            this.form.delFlag=0;
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认删除质检编号为"' + checkIds + '"的数据项？').then(function() {
        return delCheck(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/check/export', {
        ...this.queryParams
      }, `check_${new Date().getTime()}.xlsx`)
    },
    getDriver(){
      getDriverList().then(res =>{
        this.driverList = res.data;
      })
    },
    getWarehouse(){
      getWarehouseList2(this.queryParams1).then(res =>{
        this.warehouseList = res.rows;
        this.total = res.total;
      })
    },
    /** 开始检测按钮操作 */
    handleCheck(row) {
      this.checkForm=row;
      this.dialogFormVisibles = true;
      this.getWarehouse();
      this.getDriver();
    },
    submitYunShu(){
      this.checkForm.driverId=cropIng.state.driverId;
      this.checkForm.beginTime=this.timeValue1[0];
      this.checkForm.endTime=this.timeValue1[1];
      // alert(this.checkForm.warehouseId);
      addYunJiance(this.checkForm).then(r => {
        this.$modal.msgSuccess("运输成功");
        this.dialogFormVisibles = false;
        this.getList();
      });
    },
  }
};
</script>

<style lang="scss">
.t_pagination_select {
  .el-pagination {
    display: flex;
    background-color: #fff;
    align-items: center;
  .el-pagination__total,
  .el-pager,
  button {
    display: flex;
    align-items: center;
  }
}
}
</style>
